package com.dykj.fireFighting.web.onenet.controller;

import com.dykj.fireFighting.web.onenet.service.WaterTopRuiService;
import com.dykj.fireFighting.web.utils.OneNetUtils;
import org.apache.dubbo.common.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping(value = "/onenet/water")
public class WaterTopRuiController {
    //拓普瑞token
    private static String topriToken = "20211213";

    @Autowired
    private WaterTopRuiService waterTopRuiService;
    /**
     * 拓普瑞压力
     */
    @RequestMapping(value = "/receive/toprui/pressure",method = RequestMethod.POST)
    @ResponseBody
    public String receivetopri(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
            if (dataRight){
                System.out.println("data receive: content" + obj.toString());
                waterTopRuiService.putTopRiPressureToMQ(obj.getMsg(),"压力");
            }else {
                System.out.println("data receive: signature error");
            }
        }else {
            System.out.println("data receive: body empty error");
        }
        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     */

    @RequestMapping(value = "/receive/toprui/pressure", method = RequestMethod.GET)
    @ResponseBody
    public String checktopri(@RequestParam(value = "msg") String msg,
                             @RequestParam(value = "nonce") String nonce,
                             @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }

    //================================================================================================================

    /**
     * 拓普瑞液位
     */
    @RequestMapping(value = "/receive/toprui/yewei",method = RequestMethod.POST)
    @ResponseBody
    public String receivetopriyewei(HttpServletRequest request) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));


        String name= IOUtils.read(reader);
        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
        if (obj != null){
            boolean dataRight = OneNetUtils.checkSignatureTest(obj.getMsg().toString(),obj.getNonce(),obj.getMsgSignature(),topriToken);
            if (dataRight){
                System.out.println("data receive: content" + obj.toString());
                waterTopRuiService.putTopRiPressureToMQ(obj.getMsg(),"液位");
            }else {
                System.out.println("data receive: signature error");
            }
        }else {
            System.out.println("data receive: body empty error");
        }
        return "ok";
    }
    /**
     * 功能说明： URL&Token验证接口。如果验证成功返回msg的值，否则返回其他值。
     */

    @RequestMapping(value = "/receive/toprui/yewei", method = RequestMethod.GET)
    @ResponseBody
    public String checktopriyewei(@RequestParam(value = "msg") String msg,
                                  @RequestParam(value = "nonce") String nonce,
                                  @RequestParam(value = "signature") String signature){
        System.out.println("msg--"+msg+"--nonce--"+nonce+"--signature"+signature);
        boolean dataRight = OneNetUtils.checkSignatureTest(msg,nonce,signature, topriToken);
        System.out.println("dataRight-----------"+dataRight);
        return msg;
    }

//    @RequestMapping(value = "/test", method = RequestMethod.GET)
//    @ResponseBody
//    public String test(){
//        String name="{\"msg\":{\"at\":1642490169670,\"imei\":\"862657051572307\",\"type\":1,\"ds_id\":\"3200_0_5750\",\"value\":\"{\\\"ID\\\":2,\\\"ACTION\\\":1,\\\"LIMIT\\\":0.3000,\\\"t\\\":\\\"20220126105631\\\",\\\"F\\\":0.6000,\\\"T\\\":20.1572,\\\"V\\\":99.79,\\\"Q\\\":24,\\\"R\\\":56}\",\"dev_id\":882256092},\"msg_signature\":\"MX4Of2CKmgyimGHCroOoyw==\",\"nonce\":\"htw8dL7h\"}";
//        OneNetUtils.BodyObj obj = OneNetUtils.resolveBody(name, false);
//        if (obj != null){
//                System.out.println("data receive: content" + obj.toString());
//                waterTopRuiService.putTopRiPressureToMQ(obj.getMsg(),"压力");
//        }else {
//            System.out.println("data receive: body empty error");
//        }
//        return "ok";
//    }
}
